﻿using System;
using System.Collections.Generic;
using System.Linq;
using StudentInfo.Areas.StudentArea.ViewModels;

namespace StudentInfo.Areas.StudentArea.ViewModelHelpers
{
    public class StudentViewModelHelper
    {
        public static StudentViewModel PopulateStudentViewModel(Student student)
        {
            return new StudentViewModel
            {
                Id = student.Id,
                StudentName = student.StudentName,
                Year = (student.Year != null) ? student.Year.Value.ToShortDateString() : string.Empty,
                MarkList = student.Marks
                        .Select(x => new MarkInfo { Id = x.Id, ExamName = x.Exam.ExamName }).ToList(),
            };
        }

        public static Student PopulateStudentFromViewModel(StudentViewModel model)
        {
            return new Student
            {
                Id = model.Id,
                StudentName = model.StudentName,
                Year = !string.IsNullOrEmpty(model.Year) ? Convert.ToDateTime(model.Year) : (DateTime?)null
            };
        }

        public static List<StudentViewModel> PopulateStudentViewModelList(List<Student> studentList)
        {
            return studentList.Select(s => PopulateStudentViewModel(s)).ToList();
        }
    }
}